package cn.easybuy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.CarDao;
import cn.easybuy.entity.Car;
import cn.easybuy.service.CarService;

public class CarDaoImpl extends BaseDao implements CarDao{

	@Override
	public int saveProduct(Car car) {
		String sql="insert into easybuy_car(`name`,shuxin,number,totalPay) values(?,?,?,?)";
		int num=this.executeUpdate(sql, car.getName(),car.getShuxin(),car.getNumber(),car.getTotalPay());
		return num;
	}

	@Override
	public int updataProduct(Car car) {
		String sql="update easybuy_car set number=?,totalPay=? where `name`=?";
		int num=this.executeUpdate(sql, car.getNumber(),car.getTotalPay(),car.getName());
		return num;
	}

	@Override
	public List<Car> getCarsByName(String name) {
		String sql="select * from easybuy_car where `name`=?";
		ResultSet rs=this.exxcuteQuery(sql, name);
		List<Car> list=new ArrayList<Car>();
		try {
			while(rs.next()){
				Car car=new Car();
				car.setId(rs.getInt(1));
				car.setName(rs.getString(2));
				car.setShuxin(rs.getString(3));
				car.setNumber(rs.getInt(4));
				car.setTotalPay(rs.getDouble(5));
				list.add(car);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Car> getCarsAll() {
		String sql="select * from easybuy_car";
		ResultSet rs=this.exxcuteQuery(sql);
		List<Car> list=new ArrayList<Car>();
		try {
			while(rs.next()){
				Car car=new Car();
				car.setId(rs.getInt(1));
				car.setName(rs.getString(2));
				car.setShuxin(rs.getString(3));
				car.setNumber(rs.getInt(4));
				car.setTotalPay(rs.getDouble(5));
				list.add(car);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public int delProduct(String name) {
		String sql="delete from easybuy_car where `name`=?";
		int num=this.executeUpdate(sql, name);
		return num;
	}

	@Override
	public int deleteCar() {
		String sql="delete from easybuy_car";
		int num=this.executeUpdate(sql);
		return num;
	}

}
